/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *  
 * @author: jin Chen 
 * @date: May 24, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.hce.clustering.similarity;

import edu.psu.geovista.app.hce.clustering.matrix.DfClusterDialogMatrixFloat;
import edu.psu.geovista.app.hce.clustering.matrix.DfDialogMatrixFloat;
import edu.psu.geovista.app.hce.clustering.matrix.DialogMatrixFloat;
import edu.psu.geovista.app.hce.clustering.model.DfLCluster;
import edu.psu.geovista.app.hce.clustering.model.LCluster;
import edu.psu.geovista.app.hce.clustering.normalize.Normalizer;
import edu.psu.geovista.app.hce.clustering.similarity.attribute.SimilarityComparator;
import edu.psu.geovista.data.model.AppDataModel;
import edu.psu.geovista.data.model.DefaultAppDataModel;

import java.util.ArrayList;
import java.util.List;

public class RowSimilarityWorker extends BasicSimilarityWorker {
    private static final boolean DEBUG=false;

    public RowSimilarityWorker() {
    }

    public DialogMatrixFloat buildMatrix(AppDataModel data){
        AppDataModel normalData = Normalizer.normalize((DefaultAppDataModel) data);//normalized
        //normalData.
        DialogMatrixFloat simMatrix = buildOnNormalData(normalData);
        simMatrix.print();
        //testPrint(simMatrix);
        return  simMatrix;

    }

    /**
     *  
     * @param data
     * @return
     */
    public DialogMatrixFloat buildOnNormalData(AppDataModel data){
        int numOfRecords = data.getRowCount();//getNumRecords();
        //float[][] matrix=new float[numOfRecords][numOfRecords];
        DialogMatrixFloat matrix=new DfDialogMatrixFloat(numOfRecords);
        for (int i=0;i<numOfRecords;i++){
            float[] row1 = data.getRowValueAsFloat(i);
            for (int j=0;j<numOfRecords;j++){
                if(j>=i){
                float[] row2 = data.getRowValueAsFloat(j);
                    SimilarityComparator simMethod = getSimMethod();
                    double sim = simMethod.similarity(row1,row2,null);//null means not equal weights
                matrix.set(i,j,(float) sim);
                    //matrix[i][j]=(float) sim;
                   //matrix[j][i]=(float) sim;
                }
            }
        }

        //set rownames
        if (matrix instanceof DfDialogMatrixFloat) {
            DfDialogMatrixFloat fmatrix = (DfDialogMatrixFloat) matrix;
               int numrow = data.getRowCount();//getNumRecords();
               for (int i=0;i<numrow;i++){
                   String rowName = data.getRowName(i);
                   fmatrix.setRowName(i,rowName);
               }
        }

        return matrix;

    }

     /**
     *  
     * @param data     
     */
    public  void initClusters(DfClusterDialogMatrixFloat simMatrix,AppDataModel data){
         int number = data.getRowCount();//getNumRecords();// # of leaf nodes
         List clusters=new ArrayList(number);
        // 
        for (int i=0;i<number;i++){
            LCluster clst=new DfLCluster(i);
            clusters.add(clst);

        }
        simMatrix.setClusters(clusters);

    }



   
}



